//
//  PromptChatDateBase.m
//  SIPphone
//
//  Created by wrt on 13-12-3.
//  Copyright (c) 2013年 wrtsoft. All rights reserved.
//  即时聊天的数据库单例

#import "PromptChatDateBase.h"

//数据库对象的单例
static PromptChatDateBase *sharePromptChatDateBase = nil;

@implementation PromptChatDateBase
{
    //从数据库中读出来的暂存的字符串
    NSString *tempUserName;
    NSString *tempIpPort;
    NSString *tempContent;
    NSString *tempTextOrImg;
    NSString *tempOutOrCome;
    NSString *tempSucceed;
    NSString *tempTime;
    NSString *tempIsRead;
}

@synthesize userNameArray = _userNameArray, ipPortArray = _ipPortArray, contentArray = _contentArray, isTextOrImgArray = _isTextOrImgArray, isOutOrCome = _isOutOrCome, isReadArray = _isReadArray, isSucceedArray = _isSucceedArray, timeArray = _timeArray, listContentArray = _listContentArray, listIpPortArray = _listIpPortArray, listIsOutOrCome = _listIsOutOrCome, listIsReadArray = _listIsReadArray, listIsSucceedArray = _listIsSucceedArray, listIsTextOrImgArray = _listIsTextOrImgArray, listTimeArray = _listTimeArray, listUserNameArray = _listUserNameArray;
//单例的初始化方法
+ (PromptChatDateBase *)sharePromptChatDB
{
    @synchronized(self)
    {
        if (sharePromptChatDateBase == nil)
        {
            sharePromptChatDateBase = [[self alloc] init];
        }
    }
    return sharePromptChatDateBase;
}

//切换表名
- (void)changeTableName:(NSString *)userName
{
    self.tableNameForUser = [[NSString alloc] init];
    self.tableNameForUser = [NSString stringWithFormat:@"prochatTable_%@",userName];
}

//创建数据库
- (void)creatDB
{
//    //在一个新线程中进行数据库存储操作
//    //查找一个可用的全局队列
//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    //将要做的操作放入队列
//    dispatch_async(queue,^{
    
        NSArray *myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *myDocPath = myPaths[0];
        NSString *filename = [myDocPath stringByAppendingPathComponent:@"promptchat.db"];
        //打开数据库
        if(sqlite3_open([filename UTF8String], &db) != SQLITE_OK )
        {
            //关闭数据库
            sqlite3_close(db);
        }
        else
        {
            //创建表
            char *error;
            NSString *createSQL = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(%@ TEXT,%@ TEXT,%@ TEXT,%@ TEXT,%@ TEXT,%@ TEXT,%@ TEXT,%@ TEXT)",self.tableNameForUser,PROCHAT_USERNAME,PROCHAT_IPPORT,PROCHAT_CONTENT,PROCHAT_ISTEXTORIMG,PROCHAT_ISOUTORCOME,PROCHAT_ISSUCCEED,PROCHAT_TIME,PROCHAT_ISREAD];
            //运行SQL语句
            if(sqlite3_exec(db, [createSQL UTF8String], NULL, NULL, &error) !=SQLITE_OK
               )
            {
                NSLog(@"建表失败！%s",error);
            }
            //关闭数据库
            sqlite3_close(db);
        }
//    });
}
//读取数据库
- (void)readDB
{
    //将所需要存数据的数组初始化
    self.userNameArray = [[NSMutableArray alloc] init];
    self.ipPortArray = [[NSMutableArray alloc] init];
    self.contentArray = [[NSMutableArray alloc] init];
    self.isTextOrImgArray = [[NSMutableArray alloc] init];
    self.isOutOrCome = [[NSMutableArray alloc] init];
    self.isSucceedArray = [[NSMutableArray alloc] init];
    self.timeArray = [[NSMutableArray alloc] init];
    self.isReadArray = [[NSMutableArray alloc] init];
    
    
    //数据库先执行一遍读
    NSArray *myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *myDocPath = myPaths[0];
    NSString *filename = [myDocPath stringByAppendingPathComponent:@"promptchat.db"];
    
    //打开数据库
    if(sqlite3_open([filename UTF8String], &db) != SQLITE_OK )
    {
        //关闭数据库
        sqlite3_close(db);
    }
    else
    {
        NSString *sqlStr = [NSString stringWithFormat:@"SELECT %@,%@,%@,%@,%@,%@,%@,%@ FROM %@ ORDER BY %@ ",PROCHAT_USERNAME,PROCHAT_IPPORT,PROCHAT_CONTENT,PROCHAT_ISTEXTORIMG,PROCHAT_ISOUTORCOME,PROCHAT_ISSUCCEED,PROCHAT_TIME,PROCHAT_ISREAD,self.tableNameForUser,PROCHAT_TIME];
        
        sqlite3_stmt *statement;
        
        //预处理
        if(sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &statement, NULL) == SQLITE_OK)
        {
            //执行
            while(sqlite3_step(statement) == SQLITE_ROW)
            {
                char *text1 = (char *)sqlite3_column_text(statement, 0);
                tempUserName = [[NSString alloc] initWithUTF8String:text1];
                
                char *text2 = (char *)sqlite3_column_text(statement, 1);
                tempIpPort = [[NSString alloc] initWithUTF8String:text2];
                
                char *text3 = (char *)sqlite3_column_text(statement, 2);
                if (text3 == NULL)
                {
                    tempContent = @"❗错误信息";
                }
                else
                {
                    tempContent = [[NSString alloc] initWithUTF8String:text3];
                }
                
                char *text4 = (char *)sqlite3_column_text(statement, 3);
                tempTextOrImg = [[NSString alloc] initWithUTF8String:text4];
                
                char *text5 = (char *)sqlite3_column_text(statement, 4);
                tempOutOrCome = [[NSString alloc] initWithUTF8String:text5];
                
                char *text6 = (char *)sqlite3_column_text(statement, 5);
                tempSucceed = [[NSString alloc] initWithUTF8String:text6];
                
                char *text7 = (char *)sqlite3_column_text(statement, 6);
                tempTime = [[NSString alloc] initWithUTF8String:text7];
                
                char *text8 = (char *)sqlite3_column_text(statement, 7);
                tempIsRead = [[NSString alloc] initWithUTF8String:text8];
                
                
                if (tempUserName.length > 0 && tempIpPort.length > 0 && tempContent.length > 0 && tempTextOrImg.length > 0 && tempOutOrCome.length > 0 && tempSucceed.length > 0 && tempTime.length > 0 && tempIsRead.length > 0)
                {
                    [self.userNameArray addObject:tempUserName];
                    [self.ipPortArray addObject:tempIpPort];
                    [self.contentArray addObject:tempContent];
                    [self.isTextOrImgArray addObject:tempTextOrImg];
                    [self.isOutOrCome addObject:tempOutOrCome];
                    [self.isSucceedArray addObject:tempSucceed];
                    [self.timeArray addObject:tempTime];
                    [self.isReadArray addObject:tempIsRead];
                }
            }
            //释放状态资源
            sqlite3_finalize(statement);
            //关闭数据库
            sqlite3_close(db);
        }
    }
}

- (void)readDBForlist
{
    //将所需要存数据的数组初始化
    self.listUserNameArray = [[NSMutableArray alloc] init];
    self.listIpPortArray = [[NSMutableArray alloc] init];
    self.listContentArray = [[NSMutableArray alloc] init];
    self.listIsTextOrImgArray = [[NSMutableArray alloc] init];
    self.listIsOutOrCome = [[NSMutableArray alloc] init];
    self.listIsSucceedArray = [[NSMutableArray alloc] init];
    self.listTimeArray = [[NSMutableArray alloc] init];
    self.listIsReadArray = [[NSMutableArray alloc] init];
    
    
    //数据库先执行一遍读
    NSArray *myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *myDocPath = myPaths[0];
    NSString *filename = [myDocPath stringByAppendingPathComponent:@"promptchat.db"];
    
    //打开数据库
    if(sqlite3_open([filename UTF8String], &db) != SQLITE_OK )
    {
        //关闭数据库
        sqlite3_close(db);
    }
    else
    {
        NSString *sqlStr = [NSString stringWithFormat:@"SELECT %@,%@,%@,%@,%@,%@,%@,%@ FROM %@ GROUP BY %@, %@ ORDER BY %@ DESC",PROCHAT_USERNAME,PROCHAT_IPPORT,PROCHAT_CONTENT,PROCHAT_ISTEXTORIMG,PROCHAT_ISOUTORCOME,PROCHAT_ISSUCCEED,PROCHAT_TIME,PROCHAT_ISREAD,self.tableNameForUser,PROCHAT_USERNAME,PROCHAT_IPPORT,PROCHAT_TIME];
        
        sqlite3_stmt *statement;
        
        //预处理
        if(sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &statement, NULL) == SQLITE_OK)
        {
            //执行
            while(sqlite3_step(statement) == SQLITE_ROW)
            {
                char *text1 = (char *)sqlite3_column_text(statement, 0);
                tempUserName = [[NSString alloc] initWithUTF8String:text1];
                
                char *text2 = (char *)sqlite3_column_text(statement, 1);
                tempIpPort = [[NSString alloc] initWithUTF8String:text2];
                
                char *text3 = (char *)sqlite3_column_text(statement, 2);
                if (text3 == NULL)
                {
                    tempContent = @"❗错误信息";
                }
                else
                {
                    tempContent = [[NSString alloc] initWithUTF8String:text3];
                }
                
                
                char *text4 = (char *)sqlite3_column_text(statement, 3);
                tempTextOrImg = [[NSString alloc] initWithUTF8String:text4];
                
                char *text5 = (char *)sqlite3_column_text(statement, 4);
                tempOutOrCome = [[NSString alloc] initWithUTF8String:text5];
                
                char *text6 = (char *)sqlite3_column_text(statement, 5);
                tempSucceed = [[NSString alloc] initWithUTF8String:text6];
                
                char *text7 = (char *)sqlite3_column_text(statement, 6);
                tempTime = [[NSString alloc] initWithUTF8String:text7];
                
                char *text8 = (char *)sqlite3_column_text(statement, 7);
                tempIsRead = [[NSString alloc] initWithUTF8String:text8];
                
                
                if (tempUserName.length > 0 && tempIpPort.length > 0 && tempContent.length > 0 && tempTextOrImg.length > 0 && tempOutOrCome.length > 0 && tempSucceed.length > 0 && tempTime.length > 0 && tempIsRead.length > 0)
                {
                    [self.listUserNameArray addObject:tempUserName];
                    [self.listIpPortArray addObject:tempIpPort];
                    [self.listContentArray addObject:tempContent];
                    [self.listIsTextOrImgArray addObject:tempTextOrImg];
                    [self.listIsOutOrCome addObject:tempOutOrCome];
                    [self.listIsSucceedArray addObject:tempSucceed];
                    [self.listTimeArray addObject:tempTime];
                    [self.listIsReadArray addObject:tempIsRead];
                }
            }
            //释放状态资源
            sqlite3_finalize(statement);
            //关闭数据库
            sqlite3_close(db);
        }
    }
}
//保存发送数据库
- (void)saveChatDBWithUserName:(NSString *)userName andIpPort:(NSString *)ipPort andContent:(NSString *)content andisTextOrImg:(NSString *)textOrImg andOutOrCome:(NSString *)outOrCome andIsSucceed:(int)succeed andIsRead:(int)read andTime:(NSString *)time
{
//    //在一个新线程中进行数据库存储操作
//    //查找一个可用的全局队列
//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    //将要做的操作放入队列
//    dispatch_async(queue,^{
        //数据库写操作
        NSArray *myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *myDocPath = myPaths[0];
        NSString *filename = [myDocPath stringByAppendingPathComponent:@"promptchat.db"];
        //打开数据库
        if(sqlite3_open([filename UTF8String], &db) != SQLITE_OK)
        {
            //关闭数据库
            sqlite3_close(db);
        }
        else
        {
            //INSERT OR REPLACE INTO tablename(text,text,...,integer) VALUES(?,?,?,?,?,?)
            NSString *sqlStr = [NSString stringWithFormat:@"INSERT OR REPLACE INTO %@(%@,%@,%@,%@,%@,%@,%@,%@) VALUES(?,?,?,?,?,?,?,?)",self.tableNameForUser,PROCHAT_USERNAME,PROCHAT_IPPORT,PROCHAT_CONTENT,PROCHAT_ISTEXTORIMG,PROCHAT_ISOUTORCOME,PROCHAT_ISSUCCEED,PROCHAT_TIME,PROCHAT_ISREAD];
            sqlite3_stmt *statment;
            //预处理
            if(sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &statment, NULL) == SQLITE_OK)
            {
                //绑定参数
                sqlite3_bind_text(statment, 1, [userName UTF8String], -1, NULL);
                sqlite3_bind_text(statment, 2, [ipPort UTF8String], -1, NULL);
                sqlite3_bind_text(statment, 3, [content UTF8String], -1, NULL);
                sqlite3_bind_text(statment, 4, [textOrImg UTF8String], -1, NULL);
                sqlite3_bind_text(statment, 5, [outOrCome UTF8String], -1, NULL);
                if (succeed == 1)
                {
                    sqlite3_bind_text(statment, 6, [@"成功" UTF8String], -1, NULL);
                }
                else
                {
                    sqlite3_bind_text(statment, 6, [@"失败" UTF8String], -1, NULL);
                }
                
                sqlite3_bind_text(statment, 7, [time UTF8String], -1, NULL);
                
                if (read == 1)
                {
                    sqlite3_bind_text(statment, 8, [@"已读" UTF8String], -1, NULL);
                }
                else
                {
                    sqlite3_bind_text(statment, 8, [@"未读" UTF8String], -1, NULL);
                }
                
                
                //执行插入
                if(sqlite3_step(statment) != SQLITE_DONE)
                {
                    NSLog(@"插入失败");
                }
            }
            
            //释放状态资源
            sqlite3_finalize(statment);
            //关闭数据库
            sqlite3_close(db);
        }
        [self readDB];
//    });
}
//数据库删除
- (void)deleteChatDBWithUserName:(NSString *)userName andIpport:(NSString *)ipport
{
    NSArray *myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *myDocPath = myPaths[0];
    NSString *filename = [myDocPath stringByAppendingPathComponent:@"promptchat.db"];
    //打开数据库
    if(sqlite3_open([filename UTF8String], &db) != SQLITE_OK)
    {
        //关闭数据库
        sqlite3_close(db);
    }
    else
    {
        NSString *sqlStr = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = '%@' AND %@ = '%@'",self.tableNameForUser,PROCHAT_USERNAME,userName,PROCHAT_IPPORT,ipport];
        char *errorMsg;
        if (sqlite3_exec(db, [sqlStr UTF8String], NULL, NULL, &errorMsg) == SQLITE_OK)
        {
            NSLog(@"OK");
        }
        //关闭数据库
        sqlite3_close(db);
    }
}
//数据库更新（更新是否已读）
- (void)updateDBforReadWithUserName:(NSString *)userName andIpport:(NSString *)ipport
{
//    //在一个新线程中进行数据库存储操作
//    //查找一个可用的全局队列
//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    //将要做的操作放入队列
//    dispatch_async(queue,^{
    
        NSArray *myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *myDocPath = myPaths[0];
        NSString *filename = [myDocPath stringByAppendingPathComponent:@"promptchat.db"];
        
        //打开数据库
        if(sqlite3_open([filename UTF8String], &db) != SQLITE_OK)
        {
            //关闭数据库
            sqlite3_close(db);
        }
        else
        {
            //INSERT OR REPLACE INTO tablename(text,text,...,integer) VALUES(?,?,?,?,?,?)
            NSString *sqlStr = [NSString stringWithFormat:@"UPDATE %@ SET %@ = '%@' WHERE %@ = '%@' AND %@ = '%@'",self.tableNameForUser,PROCHAT_ISREAD,@"已读",PROCHAT_USERNAME,userName,PROCHAT_IPPORT,ipport];
            sqlite3_stmt *statment;
            //预处理
            if(sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &statment, NULL) == SQLITE_OK)
            {
                //执行更新
                if(sqlite3_step(statment) != SQLITE_DONE)
                {
                    NSLog(@"更新失败");
                }
            }
            //释放状态资源
            sqlite3_finalize(statment);
            //关闭数据库
            sqlite3_close(db);
        }
        [self readDB];
//    });
}

//获取未读的条数
- (int)countForUnreadWith
{
    NSArray *myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *myDocPath = myPaths[0];
    NSString *filename = [myDocPath stringByAppendingPathComponent:@"promptchat.db"];
    
    int i = 0;
    //打开数据库
    if(sqlite3_open([filename UTF8String], &db) != SQLITE_OK)
    {
        //关闭数据库
        sqlite3_close(db);
    }
    else
    {
        //INSERT OR REPLACE INTO tablename(text,text,...,integer) VALUES(?,?,?,?,?,?)
        NSString *sqlStr = [NSString stringWithFormat:@"SELECT COUNT(*) FROM %@ WHERE %@ = '%@'",self.tableNameForUser,PROCHAT_ISREAD,@"未读"];
        sqlite3_stmt *statment;
        //预处理
        if(sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &statment, NULL) == SQLITE_OK)
        {
            while (sqlite3_step(statment) == SQLITE_ROW)
            {
                i = sqlite3_column_int(statment, 0);
            }
        }
        //释放状态资源
        sqlite3_finalize(statment);
        //关闭数据库
        sqlite3_close(db);
    }
    return i;
}

//用用户名和ip获取未读的条数
- (int)countForUnreadWithUserName:(NSString *)username andIpport:(NSString *)ip
{
    NSArray *myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *myDocPath = myPaths[0];
    NSString *filename = [myDocPath stringByAppendingPathComponent:@"promptchat.db"];
    
    int i = 0;
    //打开数据库
    if(sqlite3_open([filename UTF8String], &db) != SQLITE_OK)
    {
        //关闭数据库
        sqlite3_close(db);
    }
    else
    {
        //INSERT OR REPLACE INTO tablename(text,text,...,integer) VALUES(?,?,?,?,?,?)
        NSString *sqlStr = [NSString stringWithFormat:@"SELECT COUNT(*) FROM %@ WHERE %@ = '%@' AND %@ = '%@' AND %@ = '%@'",self.tableNameForUser,PROCHAT_ISREAD,@"未读",PROCHAT_USERNAME,username,PROCHAT_IPPORT,ip];
        sqlite3_stmt *statment;
        //预处理
        if(sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &statment, NULL) == SQLITE_OK)
        {
            while (sqlite3_step(statment) == SQLITE_ROW)
            {
                i = sqlite3_column_int(statment, 0);
            }
        }
        //释放状态资源
        sqlite3_finalize(statment);
        //关闭数据库
        sqlite3_close(db);
    }
    return i;
}

//用用户名和ip获取部分信息
- (void)readDBForpart:(NSString *)userName andIpport:(NSString *)ip andBeginNumber:(int)begin
{
    self.partUserNameArray = [[NSMutableArray alloc] init];
    self.partIpPortArray = [[NSMutableArray alloc] init];
    self.partIsTextOrImgArray = [[NSMutableArray alloc] init];
    self.partContentArray = [[NSMutableArray alloc] init];
    self.partIsOutOrCome = [[NSMutableArray alloc] init];
    self.partIsSucceedArray = [[NSMutableArray alloc] init];
    self.partIsReadArray = [[NSMutableArray alloc] init];
    self.partTimeArray = [[NSMutableArray alloc] init];
    
    //数据库先执行一遍读
    NSArray *myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *myDocPath = myPaths[0];
    NSString *filename = [myDocPath stringByAppendingPathComponent:@"promptchat.db"];
    
    //打开数据库
    if(sqlite3_open([filename UTF8String], &db) != SQLITE_OK )
    {
        //关闭数据库
        sqlite3_close(db);
    }
    else
    {
        NSString *sqlStr = [NSString stringWithFormat:@"SELECT %@,%@,%@,%@,%@,%@,%@,%@ FROM %@ WHERE %@ = '%@' AND %@ = '%@' ORDER BY %@ desc limit %d,15",PROCHAT_USERNAME,PROCHAT_IPPORT,PROCHAT_CONTENT,PROCHAT_ISTEXTORIMG,PROCHAT_ISOUTORCOME,PROCHAT_ISSUCCEED,PROCHAT_TIME,PROCHAT_ISREAD,self.tableNameForUser,PROCHAT_USERNAME,userName,PROCHAT_IPPORT,ip,PROCHAT_TIME,begin];
        
        sqlite3_stmt *statement;
        
        //预处理
        if(sqlite3_prepare_v2(db, [sqlStr UTF8String], -1, &statement, NULL) == SQLITE_OK)
        {
            //执行
            while(sqlite3_step(statement) == SQLITE_ROW)
            {
                char *text1 = (char *)sqlite3_column_text(statement, 0);
                tempUserName = [[NSString alloc] initWithUTF8String:text1];
                
                char *text2 = (char *)sqlite3_column_text(statement, 1);
                tempIpPort = [[NSString alloc] initWithUTF8String:text2];
                
                char *text3 = (char *)sqlite3_column_text(statement, 2);
                if (text3 == NULL)
                {
                    tempContent = @"❗错误信息";
                }
                else
                {
                    tempContent = [[NSString alloc] initWithUTF8String:text3];
                }
                
                char *text4 = (char *)sqlite3_column_text(statement, 3);
                tempTextOrImg = [[NSString alloc] initWithUTF8String:text4];
                
                char *text5 = (char *)sqlite3_column_text(statement, 4);
                tempOutOrCome = [[NSString alloc] initWithUTF8String:text5];
                
                char *text6 = (char *)sqlite3_column_text(statement, 5);
                tempSucceed = [[NSString alloc] initWithUTF8String:text6];
                
                char *text7 = (char *)sqlite3_column_text(statement, 6);
                tempTime = [[NSString alloc] initWithUTF8String:text7];
                
                char *text8 = (char *)sqlite3_column_text(statement, 7);
                tempIsRead = [[NSString alloc] initWithUTF8String:text8];
                
                
                if (tempUserName.length > 0 && tempIpPort.length > 0 && tempContent.length > 0 && tempTextOrImg.length > 0 && tempOutOrCome.length > 0 && tempSucceed.length > 0 && tempTime.length > 0 && tempIsRead.length > 0)
                {
//                    [self.partUserNameArray addObject:tempUserName];
//                    [self.partIpPortArray addObject:tempIpPort];
//                    [self.partContentArray addObject:tempContent];
//                    [self.partIsTextOrImgArray addObject:tempTextOrImg];
//                    [self.partIsOutOrCome addObject:tempOutOrCome];
//                    [self.partIsSucceedArray addObject:tempSucceed];
//                    [self.partTimeArray addObject:tempTime];
//                    [self.partIsReadArray addObject:tempIsRead];
                    
                    [self.partUserNameArray insertObject:tempUserName atIndex:0];
                    [self.partIpPortArray insertObject:tempIpPort atIndex:0];
                    [self.partContentArray insertObject:tempContent atIndex:0];
                    [self.partIsTextOrImgArray insertObject:tempTextOrImg atIndex:0];
                    [self.partIsOutOrCome insertObject:tempOutOrCome atIndex:0];
                    [self.partIsSucceedArray insertObject:tempSucceed atIndex:0];
                    [self.partTimeArray insertObject:tempTime atIndex:0];
                    [self.partIsReadArray insertObject:tempIsRead atIndex:0];
                }
            }
            //释放状态资源
            sqlite3_finalize(statement);
            //关闭数据库
            sqlite3_close(db);
        }
    }
    
}


@end
